package com.baseinfinityrun;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import android.util.Log;

import com.badlogic.androidgames.framework.Game;
import com.badlogic.androidgames.framework.Screen;
import com.badlogic.androidgames.framework.Input.TouchEvent;
import com.badlogic.androidgames.framework.gl.Camera2D;
import com.badlogic.androidgames.framework.gl.SpriteBatcher;
import com.badlogic.androidgames.framework.impl.GLGame;
import com.badlogic.androidgames.framework.impl.GLGraphics;
import com.badlogic.androidgames.framework.math.OverlapTester;
import com.badlogic.androidgames.framework.math.Vector2;
import com.buttons.Button;
import com.buttons.Menu;

public class MainMenuScreen extends Screen
{
	private final float FRUSTUM_WIDTH = 4.8f;
	private final float FRUSTUM_HEIGHT = 3.2f;
	
	Button menu;
	
	Camera2D camera;
	SpriteBatcher batcher;
	GLGraphics glGraphics;
	Vector2 touchPoint;
	
	public MainMenuScreen(Game game) 
	{
		super(game);	
		touchPoint = new Vector2();
		glGraphics = ((GLGame)game).getGLGraphics();
		camera = new Camera2D(glGraphics,FRUSTUM_WIDTH,FRUSTUM_HEIGHT);		
		batcher = new SpriteBatcher(glGraphics, 10);
		menu = new Menu(FRUSTUM_WIDTH/2,FRUSTUM_HEIGHT/2,FRUSTUM_WIDTH/4,FRUSTUM_HEIGHT/4);
	}

	@Override
	public void update(float deltaTime) 
	{
		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
		
		int len = touchEvents.size();
		for(int i = 0; i < len; i++)
		{
			TouchEvent ev = touchEvents.get(i);
			touchPoint.set(ev.x,ev.y);
			camera.touchToWorld(touchPoint);
			if(OverlapTester.pointInRectangle(menu.bounds, touchPoint))
			{
				game.setScreen(new GameScreen(this.game));
			}
		}
	}

	@Override
	public void present(float deltaTime)
	{
		GL10 gl = glGraphics.getGL();
	    gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
	    camera.setViewportAndMatrices();
	        
	    gl.glEnable(GL10.GL_BLEND);
	    gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
	    gl.glEnable(GL10.GL_TEXTURE_2D);
	    menu.Draw(batcher);
	    gl.glDisable(GL10.GL_BLEND);
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

}
